#!/usr/bin/python
# -*- coding: utf-8 -*-

from datetime import datetime
from os import chmod
import md5

class rss:

    def __init__(self):
        self.TERM="none"
        self.GROUPS_NAME = "none"
        self.DISCIPLINE_CODE = "MC999"
        self.DISCIPLINE_NAME = "Disciplina Inválida"
        self.BASEDIR=".."
        self.MODE = 0
        self.NEWS = []
        self.URL = "http://www.fakedomain.com"

    def generate_rss(self):
        rss = """<?xml version="1.0" encoding="utf-8"?>
        <rss version="2.0">
        <channel>
        <title>%s</title>
        <link>%s</link>
        <description>%s</description>
        <lastBuildDate>%s</lastBuildDate>
        <language>pt-br</language>""" % (
            self.DISCIPLINE_CODE,
            self.URL,
            "%s - %s - %s - Turmas %s" % (self.DISCIPLINE_CODE,
                                          self.DISCIPLINE_NAME,
                                          self.TERM,
                                          self.GROUPS_NAME),
            datetime.now().strftime("%d %b %Y %H:%M:%S -0300")
            )
        
        pattern = {}
        pattern["susy"] = "Susy"
        pattern["scores"] = "notas" 


        self.NEWS.sort()
        self.NEWS.reverse()
        for i in self.NEWS:
            rss = rss + """<item>
            <title>%s</title>
            <link>%s</link>
            <guid isPermaLink=\"false\">%s</guid>
            <pubDate>%s</pubDate>
            <description>%s</description>
            </item>""" % (i[2],
                          self.URL,
                          md5.new("%02d%02d %s %s %s" % (i[0],i[1],
                                                         i[2], i[3],
                                                         self.URL)
                                  ).hexdigest(),
                          datetime(2007, i[0], i[1]).
                          strftime("%d %b %Y "\
                                   "%H:%M:%S -0300"),                          
                          i[3] % pattern)

        rss = rss + "</channel></rss>"

        arq = open("%s/index.xml" % (self.HTMLDIR), "w")
        arq.write(rss)
        arq.close()

        chmod("%s/index.xml" % (self.HTMLDIR), self.MODE)

        
